package dao;

import java.util.Collection;
import java.util.List;

import model.person.Person;
import model.person.post.Post;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class MyDAO extends HibernateDaoSupport {

	public void saveOrUpdateEntity(Object o) {
		getHibernateTemplate().saveOrUpdate(o);
		getHibernateTemplate().flush();
	}

	public <T> T getEntity(Class<T> clazz, long id) {
		return getHibernateTemplate().get(clazz, id);
	}

	public void deleteEntity(Object object) {
		getHibernateTemplate().delete(object);
	}

	public <T> List<T> getAllEntitiesWhereIn(Class<T> clazz, String param, Collection<Long> collection) {
		StringBuffer sb = new StringBuffer();
		sb.append("FROM " + clazz.getName() + " WHERE " + param + " IN (");
		for (Long l : collection) {
			sb.append(l + ",");
		}
		sb.append("0) ORDER BY ID DESC");

		return getHibernateTemplate().find(sb.substring(0));
	}

	public <T> List<T> getAllEntities(Class<T> clazz) {
		return getHibernateTemplate().find("FROM " + clazz.getName() + " v");
	}

	public <T> List<T> getAllEntitiesAndOrderBy(Class<T> clazz, String by) {
		return getHibernateTemplate().find("FROM " + clazz.getName() + " v ORDER BY " + by);
	}

	public <T> List<T> getAllEntitiesAndOrderBy(Class<T> clazz, String by, boolean asc) {
		if (asc) {
			return getHibernateTemplate().find("FROM " + clazz.getName() + " v ORDER BY " + by + " ASC");
		} else {
			return getHibernateTemplate().find("FROM " + clazz.getName() + " v ORDER BY " + by + " DESC");
		}

	}

	public <T> T getEntityWhere(Class<T> clazz, String parameter, String value) {
		String hqlQuery = "FROM " + clazz.getName() + " WHERE " + parameter + "='" + value + "'";
		List<T> l = getHibernateTemplate().find(hqlQuery);
		if (l.size() == 0) {
			return null;
		} else {
			return l.get(0);
		}
	}

	public <T> T getEntityWhereXAndY(Class<T> clazz, String parameter, String value, String parameter2, String value2) {
		String hqlQuery = "FROM " + clazz.getName() + " WHERE " + parameter + "='" + value + "' AND " + parameter2 + "='" + value2 + "'";
		List<T> l = getHibernateTemplate().find(hqlQuery);
		if (l.size() == 0) {
			return null;
		} else {
			return l.get(0);
		}
	}

	public <T> List<T> getAllEntitiesWhere(Class<T> clazz, String parameter, String value) {
		String hqlQuery = "FROM " + clazz.getName() + " WHERE " + parameter + "='" + value + "'";
		List<T> l = getHibernateTemplate().find(hqlQuery);
		return getHibernateTemplate().find(hqlQuery);
	}

	public <T> List<T> getAllEntitiesWhereAndOrderBy(Class<T> clazz, String parameter, String value, String orderBy, boolean asc) {
		String hqlQuery;
		if (asc) {
			hqlQuery = "FROM " + clazz.getName() + " WHERE " + parameter + "='" + value + "' ORDER BY " + orderBy + " ASC";
		} else {
			hqlQuery = "FROM " + clazz.getName() + " WHERE " + parameter + "='" + value + "' ORDER BY " + orderBy + " DESC";
		}
		List<T> l = getHibernateTemplate().find(hqlQuery);
		return getHibernateTemplate().find(hqlQuery);
	}

	public <T> List<T> getAllEntitiesSimilarTo(Class<T> clazz, String parameter, String value) {
		String hqlQuery = "FROM " + clazz.getName() + " WHERE lower(" + parameter + ") LIKE lower('" + value + "')";
		return getHibernateTemplate().find(hqlQuery);
	}

	public <T> List<T> getAllEntitiesByCriteria(Class<T> clazz, DetachedCriteria criteria) {
		return getHibernateTemplate().findByCriteria(criteria);
	}

}
